package service;

import common.util.PageBean;
import mapper.RegistryMapper;
import model.dto.RegistryDto;
import model.dto.SysUserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @className: service.RegistryServiceImpl
 * @author: Xia
 * @create: 2023-03-15 21:10
 */
@Service
public class RegistryServiceImpl implements RegistryService{
    @Autowired
    private RegistryMapper mapper;
    @Override
    public PageBean<RegistryDto> page(String patientIdCard, String departmentName, String userName, Integer status, Integer index) {
        PageBean<RegistryDto> pageBean = new PageBean<>();
//            1.获取数据总条数
        Integer count = mapper.getRegisterCount();
        pageBean.setTotal(count);
//            2.获取总页数
        Integer totalPage = count % pageBean.pageSize == 0 ? count / pageBean.pageSize : count / pageBean.pageSize+1;
//            3.判断如果首页数大于总页数的话，首页数=总页数；
        if(index>totalPage){
            index=totalPage;
        }
//            4.判断如果首页数小于1的话，首页数=1
        if(index<1){
            index=1;
        }
        Integer start = (index-1)*pageBean.pageSize;
        List<RegistryDto> users = mapper.getRegisterList(patientIdCard,departmentName,userName,status,start,pageBean.pageSize);
        pageBean.setPages(totalPage);
        pageBean.setData(users);
        pageBean.setPageNo(index);
        return pageBean;
    }

    @Override
    public RegistryDto getRegistryById(Long id) {
        return mapper.getRegistryById(id);
    }
}
